package net.ib.mtalk.ui.buddy.talk;

import net.ib.mntalk.protocol.Session.AddContactsReq;
import net.ib.mntalk.protocol.Session.GetBuddyChangesReq;
import net.ib.mntalk.protocol.Session.GetBuddyInfosReq;
import net.ib.mntalk.protocol.Session.GetContactsReq;
import net.ib.mntalk.protocol.Session.RemoveBuddiesReq;
import net.ib.mntalk.protocol.Session.SearchBuddyReq;
import net.ib.mntalk.protocol.Session.SetBuddyInfoReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.database.helper.BuddyDatabaseHelper;
import net.ib.mtalk.network.NetResultReceiver;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.task.AddContactsTask;
import net.ib.mtalk.network.task.AllAddContactsTask;
import net.ib.mtalk.network.task.GetBuddyChangesTask;
import net.ib.mtalk.network.task.GetBuddyInfosTask;
import net.ib.mtalk.network.task.SearchBuddyTask;
import net.ib.mtalk.network.task.SetBuddyinfoTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class BuddyTalkController extends UIController {
	
	private NetResultReceiver mReceiver;
	
	private Handler mUiHandler = new Handler();
	
	private NotifyingAsyncQueryHandler mQueryHandler;
	private GetContactsReq getContactsReq;

	public BuddyTalkController(Context context, Handler handler) {
		super(context, handler);
		mUiHandler = handler;
	}
	
	public void getCursor(){
		try {
			MTalkLogWriter.d("BuddyTalkController.getBuddyChanges()");
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getBuddyChanges", e);
		}
	}
	
	
	/**
	 * Add all Contacts at Server
	 */
	public void addAllContacts(){
		try{
			MTalkLogWriter.d("BuddyTalkController.addAllContacts()");
			AllAddContactsTask task = new AllAddContactsTask(mContext, mUiHandler);
			task.execute();
		} catch(Exception e) {
			Log.e(MTalkConstant.TAG, "addAllContacts", e);
		}
	}
	
	/**
	 * buddyChangeContacts 얻기
	 * @param GetBuddyChangesReq
	 */
	public void getBuddyChanges(GetBuddyChangesReq getBuddyChangesReq){
		try {
			MTalkLogWriter.d("BuddyTalkController.getBuddyChanges()");
			GetBuddyChangesTask task = new GetBuddyChangesTask(mContext, mUiHandler, getBuddyChangesReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getBuddyChanges", e);
		}
	}
	
	/**
	 * Buddy Info 조회
	 * @param GetBuddyInfosReq
	 */
	public void getBuddyInfos(GetBuddyInfosReq getBuddyInfosReq){
		try {
			MTalkLogWriter.d("BuddyTalkController.getBuddyInfos()");
			GetBuddyInfosTask task = new GetBuddyInfosTask(mContext, mUiHandler, getBuddyInfosReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "getBuddyInfos", e);
		}
	}

	/**
	 * Buddy의 info set
	 * 
	 */
	public void setBuddyInfo(SetBuddyInfoReq setBuddyInfoReq){
		/*try {
			BuddyDatabaseHelper.updateBuddy(mQueryHandler, 
					propertytype, 
					buddyInfo);
			
			TcpEnvelope rev = new TcpEnvelope.Builder()
				.setType(TcpEnvelope.REQUEST)
				.setMethodId(MethodId.User.SetUserProfile)
				.setCallback(mReceiver)
				.build();
			TcpClient.send(rev);
		} catch (Exception e) {
			e.printStackTrace();
		}*/
		try {
			MTalkLogWriter.d("BuddyTalkController.setBuddyInfo()");
			SetBuddyinfoTask task = new SetBuddyinfoTask(mContext, mUiHandler, setBuddyInfoReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "setBuddyInfo", e);
		}
		
	}
	
	public void removeBuddies(RemoveBuddiesReq buddiesReq){
		// TODO 
	}
	
	public void onNetResult(int resultCode, Object request, Object result) {
		TcpEnvelope rev = (TcpEnvelope) result;
		switch (rev.getMethodId()) {
			case Session.User.SetUserProfile:
				resultSetBuddyInfo();
				break;

		default:
			break;
		}
	}

	private void resultSetBuddyInfo() {
		// TODO Auto-generated method stub
		
	}
	public void onNetError(int resultCode, Object resultData) {
	}

	/**
	 * searchBuddy()
	 * @param searchBuddyReq
	 */
	public void searchBuddy(SearchBuddyReq searchBuddyReq) {
		try {
			MTalkLogWriter.d("BuddyTalkController.searchBuddy()");
			SearchBuddyTask task = new SearchBuddyTask(mContext, mUiHandler, searchBuddyReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "searchBuddy", e);
		}
	}
}
